package com.cq.jackmar.rxdemo.demo;

import android.os.Bundle;

import com.cq.jackmar.rxdemo.R;
import com.cq.jackmar.rxdemo.base.BaseActivity;
import com.cq.jackmar.rxdemo.http.base.BaseResult;
import com.cq.jackmar.rxdemo.http.httphelper.HttpHelperUser;
import com.cq.jackmar.rxdemo.http.subscriber.IOnNextListener;
import com.cq.jackmar.rxdemo.http.subscriber.RcListSubscriber;
import com.cq.jackmar.rxdemo.view.rvlist.IOnRefreshListener;
import com.cq.jackmar.rxdemo.view.rvlist.RefreshUtil;
import com.cq.jackmar.rxdemo.view.rvlist.view.JRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * Created by JackMar on 2017/3/1.
 * 邮箱：1261404794@qq.com
 */
public class PtrRecyclerViewActivity extends BaseActivity implements IOnRefreshListener {
    @Bind(R.id.recyclerView)
    JRecyclerView mRecyclerView;
    @Bind(R.id.ptrLayout)
    PtrClassicFrameLayout mPtrLayout;
    private CommonAdapter<String> mAdapter;
    private RefreshUtil refreshUtil;
    private List<String> strings = new ArrayList<>();
    private int pageIndex = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list_base;
    }

    @Override
    protected void initView() {
        refreshUtil = new RefreshUtil(context, mPtrLayout, mRecyclerView);
        refreshUtil.setDefaultDivider();
        refreshUtil.setOnRefreshListener(this);
        mAdapter = new CommonAdapter<String>(context, R.layout.cell_list, strings) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                //绑定数据
                holder.setText(R.id.tv_text, "测试数据" + (position + 1));
            }
        };
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        sendNet();
    }

    private void sendNet() {
        HttpHelperUser.getInstance().test(new RcListSubscriber<BaseResult>(new IOnNextListener<BaseResult>() {
            @Override
            public void onNext(BaseResult baseResult) {
                if (pageIndex == 1) {
                    strings.clear();
                }
                addData();
                mAdapter.notifyDataSetChanged();
                pageIndex++;
            }
        }, context, refreshUtil));
    }

    private void addData() {
        for (int i = 0; i < 20; i++) {
            strings.add("");
        }
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        sendNet();
    }

    @Override
    public void onLoad() {
        sendNet();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

}
